app.controller("z-schedulWeek-c", [ '$scope','$rootScope','$z-http','$sce','z-schedulWeek-s','z-dictionary-s','z-corporation-s','z-department-s','z-punchTime-s','z-employee-s','z-holiday-s','z-edit-menu-s','$filter',
		function($scope,$rootScope,$zhttp,$sce,Service,DictionaryService,CorporationService,DepartmentService,PunchTimeService,EmployeeService,HolidayService,MenuService,$filter) {
		$scope.viewModel = {};
		$scope.viewModel.perPage = "10";
		$scope.viewModel.rowCount = "1";	
		$scope.viewModel.schedulWeek="";
		$scope.viewModel.currentDate= new Date();
		$scope.viewModel.showSaveBtn=true; //控制保存按钮是否显示
		
		$scope.editModel = {};
		$scope.editModel.title="";
		$scope.editModel.bodyHeight=null;
		$scope.editModel.schedulWeek={};
		$scope.editModel.schedulWeek.employeePunchTimeList=[];
        $scope.editModel.weeks = new Array("日", "一", "二", "三", "四", "五", "六");
        $scope.editModel.year=null;
        $scope.editModel.month=null;
        $scope.editModel.weeksList=[];
        $scope.editModel.operationType=1;
        
		$scope.viewModel.yearData = [];
		var newYear = new Date().getFullYear();
		for(var year = newYear-10;year<=newYear+10;year++ ){
			$scope.viewModel.yearData.push(year);
		}
        
        //循环设置周
        for(var i=1;i<32;i++){
            var data={};
            data.weeksNumber=i;
            data.weeksName="";
            $scope.editModel.weeksList.push(data);
        }
        
        
		//日期控件
		$scope.viewModel.dateOptions = {
				formatYear: 'yy',
			    startingDay: 1,
			    showWeeks:false
		};
		//日期控件
		$scope.viewModel.format = "yyyy/MM/dd";
		//日期启动开关
		$scope.viewModel.fromDateOpened= false;
		$scope.viewModel.thruDateOpened= false;
		
		//查询页面名称
		MenuService.getMenuByUrl({url:'schedulWeek'},
		function(data){	
			$scope.viewModel.menuList=data;
		},function(error) {});
		
		//起止日期选择
		$scope.viewModel.fromDateOpen = function($event) {
			if($scope.editModel.schedulWeek.swId!=null){
				return;
			}
			$event.preventDefault();
			$event.stopPropagation();
			if($scope.viewModel.thruDateOpened){
				$scope.viewModel.thruDateOpened=false;
			}
			$scope.viewModel.fromDateOpened = !$scope.viewModel.fromDateOpened;
		};
		
		$scope.viewModel.thruDateOpen = function($event) {
			if($scope.editModel.schedulWeek.swId!=null){
				return;
			}
			$event.preventDefault();
			$event.stopPropagation();
			if($scope.viewModel.fromDateOpened){
				$scope.viewModel.fromDateOpened=false;
			}
			$scope.viewModel.thruDateOpened = !$scope.viewModel.thruDateOpened;
		};
		
		//加载考勤状态数据
		$scope.viewModel.bindData = function(){
			$scope.viewModel.isLoading=true;
			Service.getSchedulWeekList({},
			function(data){
				$scope.viewModel.dataList = data;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$scope.viewModel.isLoading=false;		
			});
		};
		$scope.viewModel.bindData();
		
		//新增
		$scope.viewModel.add = function(swType){
			$scope.editModel.type = true;
			$scope.viewModel.showSaveBtn=true; //控制保存按钮是否显示
			$scope.editModel.schedulWeek={};
			$scope.editModel.schedulWeek.swType=swType;
			$scope.editModel.schedulWeek.swMonday=0;
			$scope.editModel.schedulWeek.swTuesday=0;
			$scope.editModel.schedulWeek.swWednesday=0;
			$scope.editModel.schedulWeek.swThursday=0;
			$scope.editModel.schedulWeek.swFriday=0;
			$scope.editModel.schedulWeek.swSaturday=0;
			$scope.editModel.schedulWeek.swSunday=0;
			$scope.editModel.schedulWeek.isDefault=0;
			$scope.editModel.schedulWeek.punchTimeId=null;
			$scope.editModel.schedulWeek.startFromTime=null;
			$scope.editModel.schedulWeek.employeeIdList=[];
			$scope.editModel.schedulWeek.dateList=[];
			$scope.editModel.schedulWeek.employeePunchTimeList=[];
			$scope.editModel.operationType=1;
			$scope.editModel.dateStr=null;
			
			$scope.viewEmployeeModel.checkboxDataList=[];
			if(swType==1){
				$scope.editModel.title="新增按周班次";
				$scope.editModel.bodyHeight=520;
				$scope.editModel.schedulWeek.isHoliday=1;
				$scope.editForm.fromDate.$dirty=false;
				$scope.editForm.thruDate.$dirty=false;
			}else{
				$scope.editModel.title="新增无规律班次";
				$scope.editModel.bodyHeight=450;
				//获取当前年份
				$scope.editModel.year = ($scope.viewModel.currentDate).getFullYear();
				var month=($scope.viewModel.currentDate).getMonth()+1+"";
				var strMonth="";
				if(month.length<=1){
					strMonth="0"+month;
				}else{
					strMonth=month;
				}
				$scope.editModel.month=strMonth;
				$scope.viewModel.weekName();	
				//日期绑定值
				$scope.editModel.day01=false;
				$scope.editModel.day02=false;
				$scope.editModel.day03=false;
				$scope.editModel.day04=false;
				$scope.editModel.day05=false;
				$scope.editModel.day06=false;
				$scope.editModel.day07=false;
				$scope.editModel.day08=false;
				$scope.editModel.day09=false;
				$scope.editModel.day10=false;
				$scope.editModel.day11=false;
				$scope.editModel.day12=false;
				$scope.editModel.day13=false;
				$scope.editModel.day14=false;
				$scope.editModel.day15=false;
				$scope.editModel.day16=false;
				$scope.editModel.day17=false;
				$scope.editModel.day18=false;
				$scope.editModel.day19=false;
				$scope.editModel.day20=false;
				$scope.editModel.day21=false;
				$scope.editModel.day22=false;
				$scope.editModel.day23=false;
				$scope.editModel.day24=false;
				$scope.editModel.day25=false;
				$scope.editModel.day26=false;
				$scope.editModel.day27=false;
				$scope.editModel.day28=false;
				$scope.editModel.day29=false;
				$scope.editModel.day30=false;
				$scope.editModel.day31=false;
			}
			//验证不能为空
			$scope.editForm.swName.$dirty=false;
			$scope.viewModel.load();
		};
		
		//修正
		$scope.viewModel.update=function(schedulWeek){
			$scope.viewModel.isLoading=true;
			$scope.editModel.type = false;
			$scope.editModel.schedulWeek=angular.copy(schedulWeek);
			$scope.editModel.schedulWeek.employeePunchTimeList=[];
			//日期绑定值
			$scope.editModel.day01=false;
			$scope.editModel.day02=false;
			$scope.editModel.day03=false;
			$scope.editModel.day04=false;
			$scope.editModel.day05=false;
			$scope.editModel.day06=false;
			$scope.editModel.day07=false;
			$scope.editModel.day08=false;
			$scope.editModel.day09=false;
			$scope.editModel.day10=false;
			$scope.editModel.day11=false;
			$scope.editModel.day12=false;
			$scope.editModel.day13=false;
			$scope.editModel.day14=false;
			$scope.editModel.day15=false;
			$scope.editModel.day16=false;
			$scope.editModel.day17=false;
			$scope.editModel.day18=false;
			$scope.editModel.day19=false;
			$scope.editModel.day20=false;
			$scope.editModel.day21=false;
			$scope.editModel.day22=false;
			$scope.editModel.day23=false;
			$scope.editModel.day24=false;
			$scope.editModel.day25=false;
			$scope.editModel.day26=false;
			$scope.editModel.day27=false;
			$scope.editModel.day28=false;
			$scope.editModel.day29=false;
			$scope.editModel.day30=false;
			$scope.editModel.day31=false;
			
			if(schedulWeek.isDefault==1){
				$scope.viewModel.showSaveBtn=false;
			}else{
				$scope.viewModel.showSaveBtn=true;
			}
			if($scope.editModel.schedulWeek.swType==1){
				$scope.editModel.title="修改按周班次";
				$scope.editModel.bodyHeight=520;
			}else{
				$scope.editModel.title="修改无规律班次";
				$scope.editModel.bodyHeight=450;
			}	
			Service.getSchedulWeekBySwId({
				swId:schedulWeek.swId
			},function(data){
				$scope.editModel.schedulWeek.swMonday=data.swMonday;
				$scope.editModel.schedulWeek.swTuesday=data.swTuesday;
				$scope.editModel.schedulWeek.swWednesday=data.swWednesday;
				$scope.editModel.schedulWeek.swThursday=data.swThursday;
				$scope.editModel.schedulWeek.swFriday=data.swFriday;
				$scope.editModel.schedulWeek.swSaturday=data.swSaturday;
				$scope.editModel.schedulWeek.swSunday=data.swSunday;
				$scope.editModel.schedulWeek.employeeIdList=data.employeeIdList;
				$scope.editModel.schedulWeek.dateList=data.dateList;
				$scope.editModel.dateStr=null;
				if($scope.editModel.schedulWeek.startFromTime!=null){
					$scope.editModel.operationType=1;
				}else{
					$scope.editModel.operationType=2;
				}
				if($scope.editModel.schedulWeek.swType==2){
					if($scope.editModel.schedulWeek.dateList.length>=0){
						var myDate = new Date($scope.editModel.schedulWeek.dateList[0]);
						$scope.editModel.year=myDate.getFullYear();
						var month=myDate.getMonth()+1+"";
						if(month.length<=1){
							$scope.editModel.month="0"+month;
						}else{
							$scope.editModel.month=month;
						}
						$scope.viewModel.weekName();
						$scope.editModel.operationSelect();
					}
				}
				if(data.fromDate!=null){
					$scope.editModel.schedulWeek.fromDate=new Date(data.fromDate);
				}
				if(data.thruDate!=null){
					$scope.editModel.schedulWeek.thruDate=new Date(data.thruDate);
				}
				$scope.viewEmployeeModel.checkboxDataList=[];
				for(var i=0;i<data.employeeIdList.length;i++){
					var employeeIdData={};
					employeeIdData.employeeId=data.employeeIdList[i];
					employeeIdData.checkbox=true;
					$scope.viewEmployeeModel.checkboxDataList.push(employeeIdData);
				}
				if($scope.editModel.schedulWeek.swType){
					$scope.editForm.fromDate.$dirty=false;
					$scope.editForm.thruDate.$dirty=false;
				}
				
				var employeePunchTimeList=data.employeePunchTimeList;
				if(employeePunchTimeList!=null){
					for(var ai=0;ai<employeePunchTimeList.length;ai++){
						for(var bi=0;bi<$scope.editModel.schedulWeek.employeePunchTimeList.length;bi++){
							if($scope.editModel.schedulWeek.employeePunchTimeList[bi].date.getTime()== employeePunchTimeList[ai].date){
								$scope.editModel.schedulWeek.employeePunchTimeList[bi].punchTimeId=employeePunchTimeList[ai].punchTimeId;
								break;
							}
						}
					}
				}
				$scope.viewModel.load();
				$scope.viewModel.isLoading=false;
			},function(error){
				$scope.viewModel.isLoading=false;
				$rootScope.alertError(error.fault.message);
			});
		};
		
		//选中事件
		$scope.viewModel.select=function(){
			$scope.viewModel.weekName();
			$scope.editModel.operationSelect();
		};

		//显示天
		$scope.viewModel.weekName=function(){
			var monthDays = new Date($scope.editModel.year, ($scope.editModel.month), 0).getDate();
			$scope.editModel.monthDays=monthDays;
			//循环设置周
			for(var i=0;i<$scope.editModel.weeksList.length;i++){
				$scope.editModel.mDate = new Date($scope.editModel.year,$scope.editModel.month - 1,$scope.editModel.weeksList[i].weeksNumber);
				$scope.editModel.weeksList[i].weeksName=$scope.editModel.weeks[$scope.editModel.mDate.getDay()];
			}
		};
		
		//加载
		$scope.viewModel.load=function(){
			$scope.viewModel.show=true;
		};
		
		//关闭加载
		$scope.viewModel.unLoad=function(){
			$scope.viewModel.show=false;
			$scope.editModel.schedulWeek={};
		};
		
		//验证
		$scope.viewModel.requiredForm= function() {
            if (!$scope.editForm.$valid) {
            	var type=true;
            	if($scope.editModel.schedulWeek.swType==1){
	            	if(!$scope.editForm.fromDate.$dirty){
	            		$scope.editForm.fromDate.$dirty=true;
	            	}
	            	if(!$scope.editForm.thruDate.$dirty){
	            		$scope.editForm.thruDate.$dirty=true;
	            	}
            	}else{
            		type=false;
            	}
            	if(!$scope.editForm.swName.$dirty){
            		$scope.editForm.swName.$dirty=true;
            	}
            	if($scope.editModel.schedulWeek.swName==null){
            		type=true;
            	}else{
            		type=false;
            	}
            	if(!type && $scope.editModel.schedulWeek.swType==2){
	            	if($scope.editModel.schedulWeek.dateList.length<=0){
	        			$rootScope.alertWarning("未设置班次日期，请选择！");
	        			return true;
	            	}
	            	//判断操作类型
	            	if($scope.editModel.operationType==1){
	            		if($scope.editModel.schedulWeek.punchTimeId==null && $scope.editModel.schedulWeek.employeeIdList.length>0){
		               		$rootScope.alertWarning("未设置考勤时间，请选择！");
		            		return true;
		            	}
		            	if($scope.editModel.schedulWeek.punchTimeId!=null && $scope.editModel.schedulWeek.employeeIdList.length<=0){
		            		$rootScope.alertWarning("未设置人员，请选择！");
		            		return true;
		            	}
	            	}else{
	            		if($scope.editModel.schedulWeek.employeeIdList.length<=0){
		            		$rootScope.alertWarning("未设置人员，请选择！");
		            		return true;
		            	}
	            		
	            		var type=false;
	            		//循环验证是否设置打卡时间
	            		for(var di=0;di<$scope.editModel.schedulWeek.dateList.length;di++){
	            			for(var ei=0;ei<$scope.editModel.schedulWeek.employeePunchTimeList.length;ei++){
	            				if($scope.editModel.schedulWeek.dateList[di].getTime()== $scope.editModel.schedulWeek.employeePunchTimeList[ei].date.getTime()){
	            					if($scope.editModel.schedulWeek.employeePunchTimeList[ei].punchTimeId==null){
	        							type=true;
	        							break;
	        						}
	            				}
	    					}
	            		}
	            		if(type){
	            			$rootScope.alertWarning("未设置考勤时间，请选择！");
		            		return true;
	            		}
	            	}
            	}
            	if($scope.editModel.schedulWeek.swType==1){
            		$scope.editForm.thruDate.$invalid=false;
            	}
            	return type;
            }else{
            	//判断操作类型
            	if($scope.editModel.operationType==1){
	            	if($scope.editModel.schedulWeek.punchTimeId==null && $scope.editModel.schedulWeek.employeeIdList.length>0){
	               		$rootScope.alertWarning("未设置考勤时间，请选择！");
	            		return true;
	            	}
	            	if($scope.editModel.schedulWeek.punchTimeId!=null && $scope.editModel.schedulWeek.employeeIdList.length<=0){
	            		$rootScope.alertWarning("未设置人员，请选择！");
	            		return true;
	            	}
	            	if($scope.editModel.schedulWeek.swType==1){
	            		//验证开始日期，必须大于当前日期
	            		/*var myDate = $scope.viewModel.currentDate;
	            		if($scope.editModel.schedulWeek.fromDate<=myDate){
	            			$rootScope.alertWarning("开始日期需要大于当前日期:"+myDate.getFullYear()+"-"+parseInt(myDate.getMonth()+1)+"-"+myDate.getDate()+"！");
	            			$scope.editForm.fromDate.$invalid=true;
	                		return true;
	            		}*/
	                	if($scope.editModel.schedulWeek.fromDate>$scope.editModel.schedulWeek.thruDate){
	                		$scope.editForm.thruDate.$invalid=true;
	                		return true;
	                	}
	                	$scope.editForm.fromDate.$invalid=false;
	                	$scope.editForm.thruDate.$invalid=false;
	            	}
            	}else{
            		if($scope.editModel.schedulWeek.employeeIdList.length<=0){
	            		$rootScope.alertWarning("未设置人员，请选择！");
	            		return true;
	            	}
            		
            		var type=false;
            		//循环验证是否设置打卡时间
            		for(var di=0;di<$scope.editModel.schedulWeek.dateList.length;di++){
            			for(var ei=0;ei<$scope.editModel.schedulWeek.employeePunchTimeList.length;ei++){
            				if($scope.editModel.schedulWeek.dateList[di].getTime()== $scope.editModel.schedulWeek.employeePunchTimeList[ei].date.getTime()){
            					if($scope.editModel.schedulWeek.employeePunchTimeList[ei].punchTimeId==null){
        							type=true;
        							break;
        						}
            				}
    					}
            		}
            		if(type){
            			$rootScope.alertWarning("未设置考勤时间，请选择！");
	            		return true;
            		}
            	}
            }
            return false;
        };
        
		//保存
		$scope.viewModel.save = function(){
			if($scope.editModel.schedulWeek.swType==2){
				$scope.editModel.schedulWeek.dateList=[];
				$scope.editModel.isDay();
				if($scope.editModel.schedulWeek.dateList.length>0){
					$scope.editModel.schedulWeek.fromDate=new Date($scope.editModel.year+"/"+$scope.editModel.month+"/"+1);
					$scope.editModel.schedulWeek.thruDate=new Date($scope.editModel.year+"/"+$scope.editModel.month+"/"+$scope.editModel.monthDays);
				}
			}
			if($scope.viewModel.requiredForm()){
				return;
			}
			//验证员工是否有其他
			Service.getVerifySchedulWeek($scope.editModel.schedulWeek,
					function(){
						$scope.viewModel.saveUpdate();
					},function(error) {
						$rootScope.alertConfirm(error.fault.message,function() {
							$scope.viewModel.saveUpdate();
						});
					});
			
		};
		
		//保存和修改
		$scope.viewModel.saveUpdate=function(){
			if($scope.editModel.type){
				$scope.viewModel.isLoading = true;
				Service.saveSchedulWeek($scope.editModel.schedulWeek,
						function(data){
							$scope.viewModel.unLoad();
							$scope.viewModel.bindData();
							$rootScope.alertInfo("新增成功。");
							$scope.viewModel.isLoading = false;
						},function(error) {
							//判断是否为数组
							if(angular.isArray(error.fault)){
								var errorData=[];
								errorData.push(error.fault[0].message);
								for(var i=0;i<error.fault[0].errorData.length;i++){
									errorData.push(error.fault[0].errorData[i]);
								}
								$rootScope.alertError(errorData);
							}else{
								$rootScope.alertError(error.fault.message);
							}
							$scope.viewModel.isLoading = false;
						});
			}else{
				Service.updateSchedulWeek($scope.editModel.schedulWeek,
						function(data){
							$scope.viewModel.unLoad();
							$scope.viewModel.bindData();
							$rootScope.alertInfo("修改成功。");
							$scope.viewModel.isLoading = false;
						},function(error) {
							//判断是否为数组
							if(angular.isArray(error.fault)){
								var errorData=[];
								errorData.push(error.fault[0].message);
								for(var i=0;i<error.fault[0].errorData.length;i++){
									errorData.push(error.fault[0].errorData[i]);
								}
								$rootScope.alertError(errorData);
							}else{
								$rootScope.alertError(error.fault.message);
							}
							$scope.viewModel.isLoading = false;
						});
			}
		};
		
		//删除
		$scope.viewModel.del=function(schedulWeek){
			$rootScope.alertConfirm("确定删除班次吗？",function() {
				if(schedulWeek.employeeSum>0){
					$rootScope.alertConfirm("是否删除班次内所分配员工排班吗？",function() {
						$scope.viewModel.isLoading=true;
						Service.deleteSchedulWeek({
							swId:schedulWeek.swId
						},function(){
							$scope.viewModel.bindData();
							$scope.viewModel.isLoading=false;
							$rootScope.alertInfo("删除成功。");
						},function(error){
							$scope.viewModel.isLoading=false;
							$rootScope.alertError(error.fault.message);
						});
					});
				}else{
					$scope.viewModel.isLoading=true;
					Service.deleteSchedulWeek({
						swId:schedulWeek.swId
					},function(){
						$scope.viewModel.bindData();
						$scope.viewModel.isLoading=false;
						$rootScope.alertInfo("删除成功。");
					},function(error){
						$scope.viewModel.isLoading=false;
						$rootScope.alertError(error.fault.message);
					});
				}
			});
		};
		
		//验证按钮是否可用
		$scope.viewModel.isDisabled=function(schedulWeek){
			if(schedulWeek.isDefault==1){
				return true;
			}
			return false;
		};
		
		//是否可用
		$scope.editModel.disabled=function(schedulWeek){
			if(schedulWeek.swId!=null){
				return true;
			}
			return false;
		};
		
		//是否可用
		$scope.editModel.monthDisabled=function(days){
			//判断如果为修改，复选框不可用
			if($scope.editModel.schedulWeek.swId!=null){
				return true;
			}
			
			//判断小于当前日期下，复选框不可用
			/*var strDate= $scope.editModel.year+"-"+$scope.editModel.month+"-"+days;
			var date=new Date(strDate);
			if(date<$scope.viewModel.currentDate){
				return true;
			}*/
			
			//判断当前最大日期，复选框不可用
			if($scope.editModel.monthDays>=days){
				return false;
			}
			else{
				return true;
			}	
		};
		
		//是否选中
		$scope.editModel.chkChecked=function(value){
			if(value==1){
				return true;
			}
			return false;
		};
		
		//点击
		$scope.editModel.isWeekCheckbox=function(number){
			if(number==1){
				if($scope.editModel.schedulWeek.swMonday==0){
					$scope.editModel.schedulWeek.swMonday=1;
				}else{
					$scope.editModel.schedulWeek.swMonday=0;
				}
			}else if(number==2){
				if($scope.editModel.schedulWeek.swTuesday==0){
					$scope.editModel.schedulWeek.swTuesday=1;
				}else{
					$scope.editModel.schedulWeek.swTuesday=0;
				}
			}else if(number==3){
				if($scope.editModel.schedulWeek.swWednesday==0){
					$scope.editModel.schedulWeek.swWednesday=1;
				}else{
					$scope.editModel.schedulWeek.swWednesday=0;
				}
			}else if(number==4){
				if($scope.editModel.schedulWeek.swThursday==0){
					$scope.editModel.schedulWeek.swThursday=1;
				}else{
					$scope.editModel.schedulWeek.swThursday=0;
				}
			}else if(number==5){
				if($scope.editModel.schedulWeek.swFriday==0){
					$scope.editModel.schedulWeek.swFriday=1;
				}else{
					$scope.editModel.schedulWeek.swFriday=0;
				}
			}else if(number==6){
				if($scope.editModel.schedulWeek.swSaturday==0){
					$scope.editModel.schedulWeek.swSaturday=1;
				}else{
					$scope.editModel.schedulWeek.swSaturday=0;
				}
			}else if(number==7){
				if($scope.editModel.schedulWeek.swSunday==0){
					$scope.editModel.schedulWeek.swSunday=1;
				}else{
					$scope.editModel.schedulWeek.swSunday=0;
				}
			}
		};
		
		//判断每月天数是否选中
		$scope.editModel.isCheckbox=function(dayNum){
			if($scope.editModel.schedulWeek.dateList!=null){
				for(var i=0;i<$scope.editModel.schedulWeek.dateList.length;i++){
					var myDate = new Date($scope.editModel.schedulWeek.dateList[i]);
					var date=myDate.getDate();
					var strDate=null;
					if(date.length<=1){
						strDate="0"+date;
					}else{
						strDate=date;
					}
					if(strDate==dayNum){
						return true;
					}
				}
				return false;
			}
		};
		
		//月份天数选中时执行
		$scope.editModel.isDay=function(){
			 if($scope.editModel.day01){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/01");
			 }
			 if($scope.editModel.day02){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/02");
			 }
			 if($scope.editModel.day03){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/03");
			 }
			 if($scope.editModel.day04){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/04");
			 }
			 if($scope.editModel.day05){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/05");
			 }
			 if($scope.editModel.day06){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/06");
			 }
			 if($scope.editModel.day07){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/07");
			 }
			 if($scope.editModel.day08){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/08");
			 }
			 if($scope.editModel.day09){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/09");
			 }
			 if($scope.editModel.day10){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/10");
			 }
			 if($scope.editModel.day11){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/11");
			 }
			 if($scope.editModel.day12){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/12");
			 }
			 if($scope.editModel.day13){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/13");
			 }
			 if($scope.editModel.day14){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/14");
			 }
			 if($scope.editModel.day15){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/15");
			 }
			 if($scope.editModel.day16){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/16");
			 }
			 if($scope.editModel.day17){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/17");
			 }
			 if($scope.editModel.day18){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/18");
			 }
			 if($scope.editModel.day19){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/19");
			 }
			 if($scope.editModel.day20){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/20");
			 }
			 if($scope.editModel.day21){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/21");
			 }
			 if($scope.editModel.day22){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/22");
			 }
			 if($scope.editModel.day23){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/23");
			 }
			 if($scope.editModel.day24){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/24");
			 }
			 if($scope.editModel.day25){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/25");
			 }
			 if($scope.editModel.day26){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/26");
			 }
			 if($scope.editModel.day27){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/27");
			 }
			 if($scope.editModel.day28){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/28");
			 }
			 if($scope.editModel.day29){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/29");
			 }
			 if($scope.editModel.day30){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/30");
			 }
			 if($scope.editModel.day31){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/31");
			 }
		};
		
		//添加日期
		$scope.editModel.addDateList=function(strDate){
			var date = new Date(Date.parse(strDate));   
			$scope.editModel.schedulWeek.dateList.push(date);
		};
		
		//选择事件
		$scope.editModel.operationSelect=function(){
			if($scope.editModel.operationType==2){
				$scope.editModel.schedulWeek.startFromTime=null;
				$scope.editModel.schedulWeek.employeePunchTimeList=[];
				if($scope.editModel.year!=null && $scope.editModel.month!=null){
					for(var ei=0;ei<$scope.editModel.monthDays;ei++){
						var number=ei+1;
						var dateStr=$scope.editModel.year+"/"+$scope.editModel.month+"/";
						if(number<=9){
							dateStr=dateStr+"0"+number;
						}else{
							dateStr=dateStr+number;
						}
						var data={};
						data.date=new Date(dateStr);
						data.punchTimeId=null;
						$scope.editModel.schedulWeek.employeePunchTimeList.push(data);
					}
				}
			}else{
				$scope.editModel.schedulWeek.employeePunchTimeList=[];
			}
		};
		
		//不同日期选择打卡时间
		$scope.editModel.operationClick=function(weeksNumber){
			$scope.editModel.dateStr=$scope.editModel.year+"/"+$scope.editModel.month+"/";
			if(weeksNumber<=9){
				$scope.editModel.dateStr=$scope.editModel.dateStr+"0"+weeksNumber;
			}else{
				$scope.editModel.dateStr=$scope.editModel.dateStr+weeksNumber;
			}
			$scope.viewPunchTimeModel.load();
		};
		
		//控制显示名称
		$scope.editModel.operationName=function(weeksNumber){
			var dateStr=$scope.editModel.year+"/"+$scope.editModel.month+"/";
			if(weeksNumber<=9){
				dateStr=dateStr+"0"+weeksNumber;
			}else{
				dateStr=dateStr+weeksNumber;
			}
			
			var date2=new Date(dateStr);
			var type=false;
			if($scope.editModel.schedulWeek.employeePunchTimeList!=null){
				//循环查询是否选中
				for(var ei=0;ei<$scope.editModel.schedulWeek.employeePunchTimeList.length;ei++){
					if($scope.editModel.schedulWeek.employeePunchTimeList[ei].date.getTime()== date2.getTime()){
						if($scope.editModel.schedulWeek.employeePunchTimeList[ei].punchTimeId!=null){
							type=true;
							break;
						}
					}
				}
			}
			
			if(type){
				return "已选";
			}else{
				return "选择";
			}
		};
		
		//控制按钮是否可用
		$scope.editModel.operationDisabled=function(weeksNumber){
			//判断如果为修改，复选框不可用
		    if($scope.editModel.schedulWeek.swId!=null){
				return true;
			}
			//判断当前最大日期，复选框不可用
			if($scope.editModel.monthDays>=weeksNumber){
				 //判断是否选中
				 if($scope.editModel.day01 && weeksNumber==1){
					 return false;
				 }else if($scope.editModel.day02 && weeksNumber==2){
					 return false;
				 }else if($scope.editModel.day03 && weeksNumber==3){
					 return false;
				 }else if($scope.editModel.day04 && weeksNumber==4){
					 return false;
				 }else if($scope.editModel.day05 && weeksNumber==5){
					 return false;
				 }else if($scope.editModel.day06 && weeksNumber==6){
					 return false;
				 }else if($scope.editModel.day07 && weeksNumber==7){
					 return false;
				 }else if($scope.editModel.day08 && weeksNumber==8){
					 return false;
				 }else if($scope.editModel.day09 && weeksNumber==9){
					 return false;
				 }else if($scope.editModel.day10 && weeksNumber==10){
					 return false;
				 }else if($scope.editModel.day11 && weeksNumber==11){
					 return false;
				 }else if($scope.editModel.day12 && weeksNumber==12){
					 return false;
				 }else if($scope.editModel.day13 && weeksNumber==13){
					 return false;
				 }else if($scope.editModel.day14 && weeksNumber==14){
					 return false;
				 }else if($scope.editModel.day15 && weeksNumber==15){
					 return false;
				 }else if($scope.editModel.day16 && weeksNumber==16){
					 return false;
				 }else if($scope.editModel.day17 && weeksNumber==17){
					 return false;
				 }else if($scope.editModel.day18 && weeksNumber==18){
					 return false;
				 }else if($scope.editModel.day19 && weeksNumber==19){
					 return false;
				 }else if($scope.editModel.day20 && weeksNumber==20){
					 return false;
				 }else if($scope.editModel.day21 && weeksNumber==21){
					 return false;
				 }else if($scope.editModel.day22 && weeksNumber==22){
					 return false;
				 }else if($scope.editModel.day23 && weeksNumber==23){
					 return false;
				 }else if($scope.editModel.day24 && weeksNumber==24){
					 return false;
				 }else if($scope.editModel.day25 && weeksNumber==25){
					 return false;
				 }else if($scope.editModel.day26 && weeksNumber==26){
					 return false;
				 }else if($scope.editModel.day27 && weeksNumber==27){
					 return false;
				 }else if($scope.editModel.day28 && weeksNumber==28){
					 return false;
				 }else if($scope.editModel.day29 && weeksNumber==29){
					 return false;
				 }else if($scope.editModel.day30 && weeksNumber==30){
					 return false;
				 }else if($scope.editModel.day31 && weeksNumber==31){
					 return false;
				 }else{
					 return true;
				 }
			}
			else{
				return true;
			}	
		};
		
		
		/************人员*************/
		//选择员工
		$scope.viewEmployeeModel={};
		$scope.viewEmployeeModel.employeeNo="";
	    $scope.viewEmployeeModel.departmentName="";
	    $scope.viewEmployeeModel.roleName="";
		$scope.viewEmployeeModel.perPage="1";
		$scope.viewEmployeeModel.rowCount="10";
		$scope.viewEmployeeModel.title="选择人员";
		$scope.viewEmployeeModel.employeeCount=0;
		// 定义当前时间
		$scope.viewEmployeeModel.effectiveDate = new Date();
		//默认员工集合
		$scope.viewEmployeeModel.checkboxDataList=[];
		
		//加载
		$scope.viewEmployeeModel.load=function(){
			$scope.viewEmployeeModel.employeeIsSeparated=null;
			$scope.viewEmployeeModel.regionType=null;
			$scope.viewEmployeeModel.employeeNo=null;
			$scope.viewEmployeeModel.roleName=null;
			$scope.viewEmployeeModel.departmentList=[];
			$scope.viewEmployeeModel.corporationList=[];
			$scope.viewEmployeeModel.employeeList=[];
			$scope.viewEmployeeModel.employeeLevelList=[];
			$scope.viewEmployeeModel.level=null;
			$scope.viewEmployeeModel.show=true;
			$scope.viewEmployeeModel.bindData();
			$(".multiselect-selected-text").text("无");
			//下拉多选框长度
			$scope.multiSelectMaxHeight=300;
			//加载信息
			$scope.viewEmployeeModel.bindSelectRegionTypeData = function(){
				//查询公司
				CorporationService.getCorporationList({
					fromDate:$scope.viewEmployeeModel.effectiveDate,
				},
				function(data){
					var site = [];  
			        $.each(data, function(index, values) {  
			            site.push({  
			                label : "("+values.corporationCode+")"+values.corporationName,  
			                value : values.corporationCode  
			            });  
			        });  
			        $('#sweCorporation').multiselect('dataprovider',site);
				},function(error) {
					
				});
			};
			$scope.viewEmployeeModel.bindSelectRegionTypeData();
			
		
			/*******使用为下拉带勾选控件*********/
			//员工级别
			$('#swelevel').multiselect({
				includeSelectAllOption: true,
				enableFiltering: true,
				delimiterText: '; ',
	            maxHeight: $scope.multiSelectMaxHeight
	        });
			//公司
			$('#sweCorporation').multiselect({
				includeSelectAllOption: true,
				enableFiltering: true,
				delimiterText: '; ',
	            maxHeight: $scope.multiSelectMaxHeight
	        });
			//公司选中事件
			$("#sweCorporation").change(function(){  
				var value=$(this).val();
			    var data={};
			    data.corporationCodeList=value;
			    data.fromDate = $scope.viewEmployeeModel.effectiveDate;
			    if(value==null){
			    	var site = [];  
			    	$('#sweDepartmentName').multiselect('dataprovider',site);
			    	$scope.viewEmployeeModel.departmentList=[];
			    }else{
			    	//查询部门
				    DepartmentService.getDepartmentListByCorporationCodeList(data,
					function(data){
				    	var site = [];  
				        $.each(data, function(index, values) {  
				            site.push({  
				                label : "("+values.departmentCode+")"+values.departmentName,  
				                value : values.departmentCode  
				            });  
				        });  
				        $('#sweDepartmentName').multiselect('dataprovider',site);
					},function(error) {
						
					});
			    }
	        });  
			
			//部门
			$('#sweDepartmentName').multiselect({
				includeSelectAllOption: true,
				enableFiltering: true,
				delimiterText: '; ',
	            maxHeight: $scope.multiSelectMaxHeight
	        });
		};
		//关闭加载
		$scope.viewEmployeeModel.unLoad=function(){
			$scope.viewEmployeeModel.show=false;
			$scope.viewEmployeeModel.employeeNo="";
		    $scope.viewEmployeeModel.departmentName="";
		    $scope.viewEmployeeModel.roleName="";
		};
		
		//加载数据
		$scope.viewEmployeeModel.bindData = function(){
			$scope.viewEmployeeModel.employeeList=[];
			if($scope.viewEmployeeModel.employeeNo!=null){
				var employeeNoData = $scope.viewEmployeeModel.employeeNo.split("\n");
				for(var i=0;i<employeeNoData.length;i++){
					$scope.viewEmployeeModel.employeeList.push(employeeNoData[i]);
				}
			}
			$scope.viewModel.isLoading=true;
			//获取基本信息
			EmployeeService.getEmployeeList({
				employeeList:$scope.viewEmployeeModel.employeeList,
				roleName:$scope.viewEmployeeModel.roleName,
				departmentList:$scope.viewEmployeeModel.departmentList,
				corporationList:$scope.viewEmployeeModel.corporationList,
				employeeLevelList:$scope.viewEmployeeModel.employeeLevelList,
				employeeIsSeparated:"0",
				perPage:$scope.viewEmployeeModel.perPage,
				rowCount:$scope.viewEmployeeModel.rowCount
			},
			function(data){
				$scope.viewEmployeeModel.dataList = data.employeeBeanList;
				$scope.viewEmployeeModel.totalData = data.pageTotal;
				$scope.viewEmployeeModel.addCheckbox();
				$scope.viewEmployeeModel.loadDefaultcheckbox();
			    $scope.viewEmployeeModel.JudgeCheckboxAll();
				$scope.viewModel.isLoading=false;
			},function(error) {
				$rootScope.alertError(error.fault.message);
				$scope.viewModel.isLoading=false;
			});
		};
		
		//选择人员保存
		$scope.viewEmployeeModel.saveEmployee=function(){
			$scope.editModel.schedulWeek.employeeIdList=[];
			for(var i=0;i<$scope.viewEmployeeModel.checkboxDataList.length;i++){
				if($scope.viewEmployeeModel.checkboxDataList[i].checkbox){
					 $scope.editModel.schedulWeek.employeeIdList.push($scope.viewEmployeeModel.checkboxDataList[i].employeeId);
				 }
			}
			$scope.viewEmployeeModel.unLoad();
		};
		
		//默认全员分配
		$scope.viewEmployeeModel.allEmployee=function(){
			//获取基本信息
			EmployeeService.getEmployeeIdList({
				employeeList:$scope.viewEmployeeModel.employeeList,
				roleName:$scope.viewEmployeeModel.roleName,
				departmentList:$scope.viewEmployeeModel.departmentList,
				corporationList:$scope.viewEmployeeModel.corporationList,
				employeeLevelList:$scope.viewEmployeeModel.employeeLevelList,
				employeeIsSeparated:"0"
			},
			function(data){
				$scope.editModel.schedulWeek.employeeIdList=data;
				$scope.viewEmployeeModel.checkboxDataList=[];
				for(var i=0;i<$scope.editModel.schedulWeek.employeeIdList.length;i++){
					var cheDate={};
					cheDate.employeeId=$scope.editModel.schedulWeek.employeeIdList[i];
					cheDate.checkbox=true;
					$scope.viewEmployeeModel.checkboxDataList.push(cheDate);
				}
				$scope.viewEmployeeModel.unLoad();
			},function(error) {
				$rootScope.alertError(error.fault.message);
			});
		};
		
		
		
		//设置默认复选框值
		$scope.viewEmployeeModel.addCheckbox=function(){
			 for(var i=0;i<$scope.viewEmployeeModel.dataList.length;i++){
				 $scope.viewEmployeeModel.dataList[i].checkbox=false;
			 }
		};
		
		//处理加载复选框选择状态
		$scope.viewEmployeeModel.loadDefaultcheckbox=function(){
			 for(var i=0;i<$scope.viewEmployeeModel.checkboxDataList.length;i++){
				 for(var j=0;j<$scope.viewEmployeeModel.dataList.length;j++){
					 if($scope.viewEmployeeModel.checkboxDataList[i].employeeId==$scope.viewEmployeeModel.dataList[j].employeeId){
						 if($scope.viewEmployeeModel.checkboxDataList[i].checkbox){
							 $scope.viewEmployeeModel.dataList[j].checkbox=true;
						 }else{
							 $scope.viewEmployeeModel.dataList[j].checkbox=false;
						 }
					 }
				 }
			 }
		};
		
		//判断是否为全选
		$scope.viewEmployeeModel.JudgeCheckboxAll=function(){
			 var type=false;
			 for(var i=0;i<$scope.viewEmployeeModel.dataList.length;i++){
				 if($scope.viewEmployeeModel.dataList[i].checkbox==false){
					 type=false;
					 break;
				 }else{
					 type=true;
				 }
			 }
			 if(type){
				 $scope.viewEmployeeModel.checkAll=true;
			 }else{
				 $scope.viewEmployeeModel.checkAll=false;
			 }
		};
		
		//全选事件
		$scope.viewEmployeeModel.checkboxAll=function(){
			//判断是否为全选
			 if($scope.viewEmployeeModel.checkAll==false){
				 angular.forEach($scope.viewEmployeeModel.dataList, function(value, key) {
					 value.checkbox=true;
					 var type=true;
					 for(var i=0;i<$scope.viewEmployeeModel.checkboxDataList.length;i++){
						 if($scope.viewEmployeeModel.checkboxDataList[i].employeeId==value.employeeId){
							 type=false;
							 break;
						 }else{
							 type=true;
						 }
					 }
					 if(type){
						 $scope.viewEmployeeModel.checkboxDataList.push(value);
					 }
				 });
			 }else{
				 angular.forEach($scope.viewEmployeeModel.dataList, function(value, key) {
					 value.checkbox=false;
					 for(var i=0;i<$scope.viewEmployeeModel.checkboxDataList.length;i++){
						 if($scope.viewEmployeeModel.checkboxDataList[i].employeeId==value.employeeId){
							 $scope.viewEmployeeModel.checkboxDataList.splice(i,1);
						 }
					 }
				 });
			 }
		};
		
		//单选事件
		$scope.viewEmployeeModel.checkbox=function(employee){
			 if(employee.checkbox){
				 employee.checkbox=false;
				 for(var i=0;i<$scope.viewEmployeeModel.checkboxDataList.length;i++){
					 if($scope.viewEmployeeModel.checkboxDataList[i].employeeId==employee.employeeId){
						 $scope.viewEmployeeModel.checkboxDataList.splice(i,1);
					 }
				 }
			 }else{
				 employee.checkbox=true;
				 $scope.viewEmployeeModel.checkboxDataList.push(employee);
			 }
			 $scope.viewEmployeeModel.JudgeCheckboxAll();
		};
		
		//控制复选框是否可用
		$scope.viewEmployeeModel.isDisabled=function(){
			if($scope.editModel.schedulWeek.isDefault==1){
				return true;
			}else{
				return false;
			}
		};

		/**************选择考勤打卡时间**************/
		$scope.viewPunchTimeModel = {};
		$scope.viewPunchTimeModel.title="选择考勤时间";
		
		//默认员工集合
		$scope.viewPunchTimeModel.radioData=null;
		
		//选中
		$scope.viewPunchTimeModel.radio=function(punchTime){
			$scope.viewPunchTimeModel.radioData=angular.copy(punchTime);
		};
		
		//加载
		$scope.viewPunchTimeModel.load=function(){
			$scope.viewPunchTimeModel.bindData();
			$scope.viewPunchTimeModel.show=true;
			//默认员工集合
			$scope.viewPunchTimeModel.radioData=null;
		};
		
		//关闭加载
		$scope.viewPunchTimeModel.unLoad=function(){
			$scope.viewPunchTimeModel.show=false;
		};
		
		//加载数据
		$scope.viewPunchTimeModel.bindData = function(){
			$scope.viewModel.isLoading=true;
			//获取基本信息
			PunchTimeService.getPunchTimeList({},
			function(data){
				$scope.viewPunchTimeModel.dataList = data;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$rootScope.alertError(error.fault.message);
				$scope.viewModel.isLoading=false;
			});
		};
		
		//单选框是否选中
		$scope.viewPunchTimeModel.rdoChecked=function(punchTime){
			if($scope.editModel.schedulWeek.punchTimeId!=null){
				if(punchTime.punchTimeId==$scope.editModel.schedulWeek.punchTimeId){
					return true;
				}
			}
			
			for(var i=0;i<$scope.editModel.schedulWeek.employeePunchTimeList.length;i++){
				if($scope.editModel.dateStr!=null){
					var date2=new Date($scope.editModel.dateStr);
					if($scope.editModel.schedulWeek.employeePunchTimeList[i].date.getTime()== date2.getTime()
							&& $scope.editModel.schedulWeek.employeePunchTimeList[i].punchTimeId==punchTime.punchTimeId){
						return true;
					}
				}
			}
			return false;
		};
		
		//选择人员保存
		$scope.viewPunchTimeModel.save=function(){
			if($scope.editModel.operationType==2){
				if($scope.editModel.dateStr!=null){
					var date2=new Date($scope.editModel.dateStr);
					for(var ei=0;ei<$scope.editModel.schedulWeek.employeePunchTimeList.length;ei++){
						if($scope.editModel.schedulWeek.employeePunchTimeList[ei].date.getTime()== date2.getTime()){
							$scope.editModel.schedulWeek.employeePunchTimeList[ei].punchTimeId=$scope.viewPunchTimeModel.radioData.punchTimeId;
							break;
						}
					}
				}
			}else{
				if($scope.viewPunchTimeModel.radioData!=null){
					$scope.editModel.schedulWeek.punchTimeId=$scope.viewPunchTimeModel.radioData.punchTimeId;
					$scope.editModel.schedulWeek.startFromTime=$scope.viewPunchTimeModel.radioData.startFromTime;
					$scope.editModel.schedulWeek.startToTime=$scope.viewPunchTimeModel.radioData.startToTime;
					$scope.editModel.schedulWeek.startInterval=$scope.viewPunchTimeModel.radioData.startInterval;
					$scope.editModel.schedulWeek.endInterval=$scope.viewPunchTimeModel.radioData.endInterval;
					$scope.editModel.schedulWeek.endFromTime=$scope.viewPunchTimeModel.radioData.endFromTime;
					$scope.editModel.schedulWeek.endToTime=$scope.viewPunchTimeModel.radioData.endToTime;
				}
			}
			$scope.viewPunchTimeModel.unLoad();
		};
		
		/**************节假日**************/
		$scope.viewHolidayModel = {};
		$scope.viewHolidayModel.title="查看节假日";
		
		//加载
		$scope.viewHolidayModel.load=function(){
			$scope.viewHolidayModel.show=true;
			$scope.viewHolidayModel.bindData();
		};
		
		//关闭加载
		$scope.viewHolidayModel.unLoad=function(){
			$scope.viewHolidayModel.show=false;
		};
		
		//加载数据
		$scope.viewHolidayModel.bindData = function(){
			$scope.viewModel.isLoading=true;
			//获取基本信息
			HolidayService.getHolidayList({
				holidayYear:(new Date()).getFullYear()
			},
			function(data){
				$scope.viewHolidayModel.dataList = data;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$rootScope.alertError(error.fault.message);
				$scope.viewModel.isLoading=false;
			});
		};
		
} ]);